################################
#
# LatexTable class
#
# This class contains a 2-D matrix, which you can automatically
# export as a latex "table environment"
#
# The data in the table can be accessed as:
# table.cells[row number][column number], where "row number" and "column number" are 
# 1-based indices (not zero-based)
#
# The zero-indices are used as row and column headers.  table.cells[0][N] produces
# the header for column N.  table.cells[N][0] produces the header for row N.
#
################################

class LatexTable:
    
    # nrows is set once, and fixed for the lifetime of this object
    def __init__(self, nrows):
        self.cells = {}
        for i in range(0, nrows+1):
            self.cells[i] = {}
            
    def appendRow(self):
        self.cells[ self.cells.__len__() ] = {}
        
    def fillEmptyCells(self):
        l = self.findLongestsRowLength()
        for i in range(0, self.cells.__len__()):
            for j in range(0,l+1):
                print "i,j ->" + i.__str__() + " " + j.__str__()
                if self.cells[i].__contains__(j) == False:
                    self.cells[i][j] = "----"
    
    def findLongestsRowLength(self):
        max = 0
        for i in range(0, self.cells.__len__()):
            k = self.cells[i].keys()
            if k.__len__() == 0:
                continue
            k.sort()
            k.reverse()
            maxcol = k[0]
            if maxcol > max:
                max = maxcol
        return max
             
    # returns the latex table as a latex-formatted string
    # Content in the '0' indices will be treated as row/col labels
    def asLatexString(self):
        self.fillEmptyCells()
        
        ts = "" # this is the object we'll return
        
        ts += "\\usepackage{rotating}\n"
        ts += "\\begin{sidewaystable}[p]\n"
        
        # the definition row:
        ts += "\\begin{tabular}{"
        ts += "l"
        for i in range(1, self.findLongestsRowLength() ):
            ts += " | r" 
        ts += " | r |}"
        ts += "\n" 
        
        # rows:
        for j in range(0,self.cells.__len__()):
            ts += self.cells[j][0].__str__()
            for i in range(1, self.cells[0].__len__()):
                ts += " & " + self.cells[j][i].__str__()
            ts += " \\\\ \\hline\n"

         
        
        # end definition
        ts += "\\end{tabular}\n"
        ts += "\\end{sidewaystable}\n"
        
        return ts
        
#        \begin{center}
#        \begin{tabular}{ l | c || r | }
#            \hline
#            1 & 2 & 3 \\ \hline
#            4 & 5 & 6 \\ \hline
#            7 & 8 & 9 \\
#            \hline
#        \end{tabular}
#        \end{center}


#################################################################################
#################################################################################
#################################################################################
#
# Here is a set of methods to test the correctness of the LatexTable. . . 
#

def test1():
    t = LatexTable(5)
    t.cells[1][1] = "monkey"
    t.cells[1][2] = "panda"
    t.cells[2][1] = "banana"
    t.cells[2][2] = "bamboo"
    t.cells[5][6] = "strange"
    
    print t.asLatexString()

def runTests():
    test1()    
    